//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.dstz.bpm.engine.action.handler.instance;

import cn.hutool.core.collection.CollectionUtil;
import com.dstz.base.api.exception.BusinessMessage;
import com.dstz.base.core.util.JsonUtil;
import com.dstz.base.core.util.StringUtil;
import com.dstz.bpm.api.constant.ActionType;
import com.dstz.bpm.api.model.nodedef.BpmNodeDef;
import com.dstz.bpm.core.manager.BpmTaskManager;
import com.dstz.bpm.core.model.BpmTask;
import com.dstz.bpm.engine.action.cmd.DefaultInstanceActionCmd;
import com.dstz.bpm.engine.action.handler.AbsActionHandler;
import com.dstz.sys.api.jms.model.DefaultJmsDTO;
import com.dstz.sys.api.jms.model.JmsDTO;
import com.dstz.sys.api.jms.model.msg.NotifyMessage;
import com.dstz.sys.api.jms.producer.JmsProducer;
import com.dstz.sys.api.model.SysIdentity;
import com.dstz.sys.util.ContextUtil;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Component;

@Component
public class InstanceReminderActionHandler extends AbsActionHandler<DefaultInstanceActionCmd> {
   @Resource
   BpmTaskManager aG;
   @Resource
   JmsProducer aH;

   public InstanceReminderActionHandler() {
   }

   @Override
   public void execute(DefaultInstanceActionCmd model) {
      String opinion = model.getOpinion();
      Boolean isUrgent = JsonUtil.getBoolean(model.getExtendConf(), "isUrgent");
      String msgType = JsonUtil.getString(model.getExtendConf(), "msgType");
      if (StringUtil.isEmpty(opinion)) {
         throw new BusinessMessage("催办提醒内容不可为空！");
      } else {
         List<BpmTask> taskList = this.aG.getByInstId(model.getInstanceId());
         if (CollectionUtil.isEmpty(taskList)) {
            throw new BusinessMessage("当前实例任务不存在，无需催办！");
         } else {
            List<SysIdentity> notifyUsers = new ArrayList();
            Iterator var7 = taskList.iterator();

            while(var7.hasNext()) {
               BpmTask task = (BpmTask)var7.next();
               List<SysIdentity> identitys = this.aG.getAssignUserById(task);
               notifyUsers.addAll(identitys);
               if (isUrgent) {
                  task.setPriority(task.getPriority() + 1);
                  this.aG.update(task);
               }
            }

            if (!CollectionUtil.isEmpty(notifyUsers) && !StringUtil.isEmpty(opinion)) {
               opinion = opinion.replaceAll("subject", ((BpmTask)taskList.get(0)).getSubject());
               List<JmsDTO> jmsDto = new ArrayList();
               String[] var14 = msgType.split(",");
               int var15 = var14.length;

               for(int var10 = 0; var10 < var15; ++var10) {
                  String type = var14[var10];
                  NotifyMessage message = new NotifyMessage("任务催办提醒", opinion, ContextUtil.getCurrentUser(), notifyUsers);
                  jmsDto.add(new DefaultJmsDTO(type, message));
               }

               this.aH.sendToQueue(jmsDto);
            }
         }
      }
   }

   public ActionType getActionType() {
      return ActionType.REMINDER;
   }

   public int getSn() {
      return 7;
   }

   public Boolean isSupport(BpmNodeDef nodeDef) {
      return false;
   }

   public Boolean isDefault() {
      return false;
   }

   public String getConfigPage() {
      return "/bpm/task/reminderActionDialog.html";
   }

   public String getDefaultGroovyScript() {
      return "return bpmInstance.getStatus().equals(\"running\");";
   }

   public String getDefaultBeforeScript() {
      return null;
   }

   protected void b(DefaultInstanceActionCmd model) {
   }

   protected void c(DefaultInstanceActionCmd data) {
   }

   protected void d(DefaultInstanceActionCmd actionModel) {
   }

   protected void e(DefaultInstanceActionCmd actionModel) {
   }

   @Override
   protected void h(DefaultInstanceActionCmd var1) {

   }

   @Override
   protected void i(DefaultInstanceActionCmd var1) {

   }
}
